// pages/mine/mine.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userName:"gggggg",
    orderData:[
      {
        id:0,
        imgUrl:'/assets/image/scanCode.png',
        title:'扫码补货'
      },
      {
        id:1,
        imgUrl:'/assets/image/xyy_yun.png',
        title:'设备列表'
      },
      {
        id:2,
        imgUrl:'/assets/image/xyy_xiaofei.png',
        title:'提现列表'
      }
    ],
    type:'',
    phone:'',
    balance:0
  },

  //点击cell
  cellClick(index){
    console.log('点击了cell')
    console.log(index.currentTarget.dataset.index)
    switch (index.currentTarget.dataset.index) {
      case "0":
        console.log('点击了0')
         //判断只有管理员才有补货权限
    if (this.data.type === 'customer'){
      wx.showToast({
        title: '您暂无此权限',
      })
      return
    }
        wx.navigateTo({
          url: '../balancelist/balancelist'
        })
        break;
        case "1":
          console.log('点击了1')
          wx.navigateTo({
            url: '../huishouOrder/huishouOrder'
          })
        break;
        case "2":
          console.log('点击了2')
          wx.showToast({
            title: '敬请期待！',
          })
        break;
        case "3":
          wx.navigateTo({
            url: '../partner/partner'
          })
        break;
        case "4":
          wx.navigateTo({
            url: '../recycler/recycler'
          })
        break;
        case "5":
          wx.navigateTo({
            url: '../addressList/addressList'
          })
        break;
        case "6":
          wx.navigateTo({
            url: '../huishouManager/huishouManager'
          })
        break;
    
      default:
        break;
    }

  },

  getUser(){
    this.requestHttp('/app/customer/info', '', 'get').then((data) => {
      console.log(data.data.type)
      console.log(data.data.phone)
      this.setData({
        type:data.data.type,
        phone:data.data.phone
      })
    })
  },

  getBalance(){
    this.requestHttp('/app/client/mine/balance', '', 'get').then((data) => {
      console.log(data.data.balance)
      this.setData({
        balance:data.data.balance,
      })
    })
  },

  categoryTap(e){
    //判断只有管理员才有补货权限
    console.log(this.data.type)
    if (this.data.type === 'customer'){
      wx.showToast({
        title: '您暂无此权限',
      })
      return
    }
    console.log(e.currentTarget.dataset.clickid)
    switch (e.currentTarget.dataset.clickid) {
      case 0://扫码查库存
        this.scanCodeEvent()
        break;
        case 1://跳转设备列表页面
        wx.navigateTo({
          url: '../devicelist/devicelist'
        })
        break;
        case 2://跳转设备列表页面
        wx.navigateTo({
          url: '../withdrawallist/withdrawallist'
        })
        break;
      default:
        break;
    }
  },

   /** 扫码事件**/
   scanCodeEvent: function(){
    var that = this;
    wx.scanCode({
      onlyFromCamera: true,// 只允许从相机扫码
      success(res){
        console.log("扫码成功："+JSON.stringify(res))
        // 扫码成功后  在此处理接下来的逻辑
        let index = res.result .lastIndexOf("=")
        res.result =res.result .substring(index+1,res.result.length);
        that.setData({
          scanCode: res.result//扫描得到的结果
        })
        console.log(that.data.scanCode)
        //跳转扫码补货页面
        wx.navigateTo({
          url: '../scandetail/scandetail?scanCode=' + that.data.scanCode
        })
      },
      fail (err) {
        console.log(err)
        wx.showToast({
          title:'扫描失败',
          icon: 'none',
          duration: 1000
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getUser()
    this.getBalance()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  requestHttp(url,data,method){
    return new Promise(function (reslove, reject) {
      let headers = {
        'Authorization': app.globalData.token || '',
        'Content-Type': 'application/json'
      };
      wx.request({
        url: app.globalData.host + url,
        method: method || 'get',
        data: data,
        header: headers,
        success: function success(res) {
          const data = res.data;
          if (data.code * 1 === 0) {
            reslove(data)
          } else {
            wx.showToast({
              title: data.msg || '网络异常',
              icon: 'error',
            })
            setTimeout(() => {
              reject(data);
            }, 10)
          }
          // reslove(res)
        },
        fail: function fail(res) {
          wx.showToast({
            title: res,
            icon: 'error',
          })
          reject(res);
        }
      })
    })
  }
})